package com.pinxixi.template.service;

import com.pinxixi.framework.domain.template.Template;
import com.pinxixi.framework.domain.template.response.TemplateCode;
import com.pinxixi.framework.model.response.*;
import com.pinxixi.template.mapper.TemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TemplateService {

    @Autowired
    private TemplateMapper templateMapper;

    public ListResponseResult<Template> findAll() {
        List<Template> list = templateMapper.findAll();
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        return new ListResponseResult<Template>(CommonCode.SUCCESS,queryResult);
    }

    public ListResponseResult<Template> findPage(Integer page, Integer size, Template template) {
        Map map = new HashMap();
        map.put("index",(page-1)*size);
        map.put("size",size);
        map.put("template",template);
        List list = templateMapper.findPage(map);
        Integer total = templateMapper.findCount(template);
        QueryResult<Template> queryResult = new QueryResult<>();
        queryResult.setList(list);
        queryResult.setTotal(total);
        return new ListResponseResult<Template>(CommonCode.SUCCESS,queryResult);
    }

    public QueryResponseResult<Template> findOne(Integer id) {
        Template template = templateMapper.findOne(id);
        if(template == null){
            return new QueryResponseResult(TemplateCode.TEMPLATE_NOT_EXISTS,null);
        }
        return new QueryResponseResult<>(CommonCode.SUCCESS,template);
    }

    public ResponseResult save(Template template) {
        if(template.getId()==null){
            templateMapper.add(template);
        }else{
            templateMapper.update(template);
        }
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public ResponseResult delelte(Integer id) {
        templateMapper.delete(id);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public ResponseResult delelteAll(Integer[] ids) {
        templateMapper.deleteAll(ids);
        return  new ResponseResult(CommonCode.SUCCESS);
    }
}
